import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from head.Init_Driver import SurveyExaminationSystem_Driver


class Login:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

    # 清空输入框
    def ClearInputBox(self):
        self.driver.find_element(By.ID,"username").clear()
        self.driver.find_element(By.ID,"password").clear()
        # 刷新页面保证所有输入框内容被清空
        self.driver.refresh()

    # 正常登录
    def LoginSucceed(self):
        # 正常输入
        self.driver.find_element(By.ID,"username").send_keys("karen")
        self.driver.find_element(By.ID,"password").send_keys("karen1234")
        self.driver.find_element(By.XPATH,"//button[span='登 录']").click()
        # 检查是否登录成功
        # assert WebDriverWait(self.driver,3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))
        # 截图保留
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 退出登录
        # self.driver.back()
        self.driver.find_element(By.CSS_SELECTOR,"span[class='anticon anticon-setting ant-dropdown-trigger']").click()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH,"//span[text()='退出登录']")))
        self.driver.find_element(By.XPATH,"//span[text()='退出登录']").click()

    # 异常登录
    def LoginFail(self):
        # 输入正确的用户名和错误的密码;预期结果：错误的用户名或密码
        self.driver.find_element(By.ID, "username").send_keys("karen")
        self.driver.find_element(By.ID, "password").send_keys("123123")
        self.driver.find_element(By.XPATH,"//button[span='登 录']").click()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH,"//div[text()='错误的用户名和密码']")))
        # 截图保留
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 清空输入框
        self.ClearInputBox()
        # 输入错误的用户名和错误的密码;预期结果：错误的用户名或密码
        self.driver.find_element(By.ID, "username").send_keys("123")
        self.driver.find_element(By.ID, "password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH,"//div[text()='错误的用户名和密码']")))

        # 清空输入框
        self.ClearInputBox()
        # 用户名为空；预期结果：用户名是必填项
        self.driver.find_element(By.ID, "username").send_keys("")
        self.driver.find_element(By.ID, "password").send_keys("123")
        self.driver.find_element(By.XPATH,"//button[span='登 录']").click()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#username_help > div")))
        # 截图保留
        time.sleep(1)
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 清空输入框
        self.ClearInputBox()
        # 密码为空；预期结果：密码是必填项
        self.driver.find_element(By.ID, "username").send_keys("123")
        self.driver.find_element(By.ID, "password").send_keys("")
        self.driver.find_element(By.XPATH,"//button[span='登 录']").click()
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#password_help > div")))
        # 截图保留
        time.sleep(1)
        SurveyExaminationSystem_Driver.Get_ScreenShot()

# login = Login()
# login.LoginSucceed()
# login.LoginFail()
# login.driver.quit()

